﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using 自制数学工具;
using Point = 自制数学工具.Point;

namespace 三角形向量计算
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Vector OA = new Vector();
        public Vector OB = new Vector();
        public Vector OC = new Vector();
        public Vector OD = new Vector();
        public Vector AB = new Vector();

        private void buttonImage_Click(object sender, EventArgs e)
        {
            ParseParameters();
            int width = 400;int heigth = 250;
            // 生成图片
            Bitmap bitmap = GenerateImage(width, heigth);

            // 创建并显示新的窗体以显示生成的图片
            ImageForm imageForm = new ImageForm(bitmap);

            imageForm.Show();
        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            ParseParameters();
            int width = 300;int heigth = 200;
            Bitmap bitmap = GenerateImage(width,heigth);
            ResultForm resultForm = new ResultForm(bitmap, new string[] { OC.X + "", OC.Y + "", OD.X + "", OD.Y + "" });
            resultForm.ShowDialog();
        }
        // 分析参数，计算结果
        private void ParseParameters()
        {
            try
            {
                string[] oa = textOA.Text.Trim(new char[] { '（', '）', ')', '(', ' ' })
                    .Split(new char[]{',','，' },StringSplitOptions.RemoveEmptyEntries);
                OA.X = int.Parse(oa[0]);OA.Y = int.Parse(oa[1]);

                string[] ob = textOB.Text.Trim(new char[] { '（', '）', ')', '(', ' ' })
                    .Split(new char[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
                OB.X = int.Parse(ob[0]); OB.Y = int.Parse(ob[1]);

                if(OA.X * OB.Y - OA.Y * OB.X == 0) {
                    throw new ArgumentException("参数异常，无法构成三角形！");
                }

                string m = textKC.Text.Trim();string n = textKD.Text.Trim();
                if(Regex.IsMatch(m,"[1-9][0-9]*") &&  Regex.IsMatch(n, "[1-9][0-9]*"))
                {
                    AB = OB.Substract(OA);
                    double k1 = 1 / double.Parse(m);
                    double k2 = 1 / double.Parse(n);
                    OC = OA.Add(AB.Multiply(k1));
                    OD = OA.Add(AB.Multiply(k2));
                    OC.X = Math.Round(OC.X, 2);OC.Y = Math.Round(OC.Y, 2);
                    OD.X = Math.Round(OD.X, 2);OD.Y = Math.Round(OD.Y, 2);
                }
                else
                {
                    throw new ArgumentException("参数异常，m或n的值应该为正整数！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 绘制示意图
        private Bitmap GenerateImage(int width, int height)
        {
            // 创建新的位图对象
            Bitmap bitmap = new Bitmap(width, height + 20);
            //三角形正形
            PointF O = new PointF(width / 2, height / 2);
            double k;
            if (OA.Length() > OB.Length())
            {
                k = (height/2)/OA.Length();
            }
            else
            {
                k = (height/2)/OB.Length();
            }
            PointF A = new PointF((float)(OA.X * k + width / 2), (float)(OA.Y * k + height / 2));
            PointF B = new PointF((float)(OB.X * k + width / 2), (float)(OB.Y * k + height / 2));
            PointF C = new PointF((float)(OC.X * k + width / 2), (float)(OC.Y * k + height / 2));
            PointF D = new PointF((float)(OD.X * k + width / 2), (float)(OD.Y * k + height / 2));
            // 使用绘图对象进行绘制
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.White);
                // 画线
                Pen pen = new Pen(Color.Black, 1);
                graphics.DrawLines(pen, new PointF[] { O, A, B , O});
                graphics.DrawLine(pen, O, C);
                graphics.DrawLine(pen, O, D);
                // 画字母
                Font font = new Font("Arial", 10);
                Brush brush = Brushes.Red;
                graphics.DrawString("A", font, brush, A.X - 10, A.Y - 5);
                graphics.DrawString("B", font, brush, B.X - 10, B.Y - 5);
                graphics.DrawString("C", font, brush, C.X - 10, C.Y - 5);
                graphics.DrawString("D", font, brush, D.X - 5, D.Y - 5);
                graphics.DrawString("O", font, brush, O.X - 10, O.Y - 5);
                // 画箭头
                graphics.DrawPolygon(pen, GetPointFs(A, width, height));
                graphics.DrawPolygon(pen, GetPointFs(B, width, height));
                graphics.DrawPolygon(pen, GetPointFs(C, width, height));
                graphics.DrawPolygon(pen, GetPointFs(D, width, height));
            }

            return bitmap;
        }

        // 获取箭头所需的点坐标组
        private PointF[] GetPointFs(PointF p,int width,int heigth)
        {
            double a = Math.Atan2(p.Y - heigth / 2, p.X - width / 2);
            double length = 10;double angle = Math.PI / 6;
            PointF end1 = new PointF(p.X, p.Y);
            PointF end2 = new PointF(p.X, p.Y);
            end1.X = (float)(end1.X - length * Math.Cos(a + angle / 2));
            end1.Y = (float)(end1.Y - length * Math.Sin(a + angle / 2));
            end2.X = (float)(end2.X - length * Math.Cos(a - angle / 2));
            end2.Y = (float)(end2.Y - length * Math.Sin(a - angle / 2));

            return new PointF[] {p , end1 , end2 };
        }
    }

    public class ImageForm : Form
    {
        private Bitmap bmp;
        private PictureBox pictureBox = new PictureBox();

        public ImageForm(Bitmap bmp)
        {
            this.bmp = bmp;
            Text = "示意图";
            MinimumSize = new Size(bmp.Width, bmp.Height + 20);
            SizeChanged += ImageForm_SizeChanged;
            init();
        }

        private void ImageForm_SizeChanged(object sender, EventArgs e)
        {
            pictureBox.Width = ClientSize.Width;
            pictureBox.Height = ClientSize.Height;
        }

        private void init()
        {
            SuspendLayout();
            pictureBox.Size = bmp.Size;
            pictureBox.Image = bmp;
            pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox.Location = new System.Drawing.Point(0, 0);

            Controls.Add(pictureBox);
        }

    }

    public class ResultForm : Form
    {
        private Bitmap bmp;
        private string[] result;
        private Size size;

        public ResultForm(Bitmap bmp, string[] result)
        {
            this.bmp = bmp;
            this.result = result;
            size = new Size(400, 340);
            ClientSize = size;
            init();
            MinimumSize = size;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawImage(bmp, 0, 0);
        }

        private void init()
        {
            lblOC = new Label();
            lblOD = new Label();
            lblOCResult = new Label();
            lblODResult = new Label();
            SuspendLayout();

            lblOC.AutoSize = true;
            lblOC.Font = new Font("宋体", 12F);
            lblOC.Location = new System.Drawing.Point(30, 230);
            lblOC.Size = new Size(34, 24);
            lblOC.Text = "OC的值：";

            lblOCResult.AutoSize = true;
            lblOCResult.Font = new Font("宋体", 12F);
            lblOCResult.Location = new System.Drawing.Point(140, 230);
            lblOCResult.Size = new Size(34, 24);
            lblOCResult.Text = $"({result[0]},{result[1]})";

            lblOD.AutoSize = true;
            lblOD.Font = new Font("宋体", 12F);
            lblOD.Location = new System.Drawing.Point(30, 280);
            lblOD.Size = new Size(34, 24);
            lblOD.Text = "OD的值：";

            lblODResult.AutoSize = true;
            lblODResult.Font = new Font("宋体", 12F);
            lblODResult.Location = new System.Drawing.Point(140, 280);
            lblODResult.Size = new Size(34, 24);
            lblODResult.Text = $"({result[2]},{result[3]})";

            Controls.Add(lblOD);
            Controls.Add(lblOC);
            Controls.Add(lblOCResult);
            Controls.Add(lblODResult);
            Text = "结果展示页";
            ResumeLayout(false);
            PerformLayout();
        }
        private Label lblOC;
        private Label lblOD;
        private Label lblOCResult;
        private Label lblODResult;

    }
}
